/*
    This is a set of libraries to assist in OpenGl and Other Applications
    Copyright (C) 2008  The Modular Engine Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestModule.h"


namespace ModularEngine
{
	void cCLEAR::init()
	{

	}


	void cCLEAR::operator ()()
	{
		TestModule::getSingleton().consoleText->setText( "" );
	}	

	int TestModule::initialize()
	{
#if 0
		ConsoleModule::getSingleton().mfConsole = &TestConsole;


		WindowModule::getSingleton().keyboardSendF = &keyboardSend;
		WindowModule::getSingleton().mouseSendF = &mouseSend;

		ServerModule::getSingleton().processMessage = &netProcServer;
		ClientModule::getSingleton().processMessage = &netProcClient;
		initWindows();

		

		console( "Console>> Modular Engine v0.4" );
#endif

		glEnable( GL_BLEND );
		glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
		ModularEngine::OpenglModule::getSingleton().clearColor( Vector3d( 0, 0, 0 ) );

		InterfaceContainer *cont = new InterfaceContainer();

		InterfaceModule::getSingleton().AddContainer( cont );
		cont->LoadSchema( "./Interface/Schemata/defaultSchema.xml");

		fps = new char[25];
		strcpy( fps, "FPS NOT YET CALC-ED." );

		bFont.loadFontFull( "fonts/Arial Small" );
		bFont2.loadFontFull( "fonts/Times New Roman", 1.0, .7 );
	
		TextEngine te;
		foxLines = 
		te.processMultiline( bFont2, 32, "The Quick Brown Fox Jumped Over the Lazy Dog", 300 );

		return 1;
	}

	int TestModule::run()
	{
		OpenglModule *gl = &ModularEngine::OpenglModule::getSingleton();

		static TextEngine te;

		glLoadIdentity();

		Vector2d bounds[2];
		bounds[0].x = 0;
		bounds[1].x = 1024;
		bounds[0].y = 0;
		bounds[1].y = 768;
		
		
		static int frames = 0;
 
		if( mTimer.timeElapsed() > 1000 )
		{
			mTimer.reset();			
			wsprintf( fps, "FPS: %d", frames );
			frames = 0;
		}

		frames++;
		
		if( InputModule::getSingleton().isKeyDown( IK_CONTROL ) )
		{
			cout << "Control Down" << endl;
		} 
		if( InputModule::getSingleton().isKeyDown( IK_SHIFT ) )
		{
			cout << " Shift Down" << endl;
		}
		//cout << endl;
		
		glColor4f( 1, 1, 1, 1 );
		//gl->drawImage( "test.tga", Vector2d( 0, -2 ), Vector2d( 1024, 768 ) );
		//GraphicsModule::getSingleton().gI->drawQuad( gl->loadTexture( "background.png" )->mTexNum, Vector2d( 0, -8), Vector2d( 1024, 1024 ) );
		GraphicsModule::getSingleton().gI->drawImage( "background.png", Vector2d( 0, 0 ) );
		//gl->drawImage( "background.png", Vector2d( 0, -8 ) );
		//gl->drawImage( "fonts/Nueva Std Cond.tga", Vector2d( 100, 100 ) );
		glColor3f( 1, 1, 1 );
		for( int i = 0; i<3; i++ )
		{
			// fonts The whole point of doing this is so that it will render much quicker and hopefully produce higher quality results well, not higher quality just... faster.... and more optimized... and more control and stufff....
			//te.drawText( bFont, 32, "0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789", Vector2d( 0, 200+i*64 ), bounds );
			te.drawText( bFont, 40, "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789", Vector2d( 0, 200+i*64 ), bounds );
			te.drawText( bFont2, 48, "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789", Vector2d( 0, 200-40+i*64 ), bounds );
		}

		te.drawTextMultiline( bFont2, 32, foxLines, Vector2d( 500, 50 ), bounds );

		//te.drawText( bFont, 32, "H", Vector2d( 0, 200+i*30 ), bounds );

		glColor3f( 1, 1, 1 );
		if( RegistryModule::getSingleton().getKey( "draw_fps" ) == 1 )
			te.drawText( bFont, 18, fps, Vector2d( 5, -5 ), bounds );

		glDisable( GL_TEXTURE_2D );

		WB.capturePoints();
		WB.drawWhiteBoard();

		return 1;

	}

	void TestModule::WhiteBoard::capturePoints()
	{
		if( WindowModule::getSingleton().isDown( 0 ) )
		{
			if( bReset )
			{
				bReset = false;
				ClientModule::getSingleton().sendMessage( GM_WBSTART );
			}

			if( mMouseTimer.timeElapsed() > 100 )
			{
				mMouseTimer.reset();
				Vector3d take;
				take = WindowModule::getSingleton().getMouse();
				ClientModule::getSingleton().sendMessage( GM_WBPOINT, (char *)&take, sizeof( Vector3d ) );
				//cout << "X: " << take.x << "Y: " << take.y << "Z: " << take.z << endl;
				//mvPoints.push_back(take);
			}
		} else {
			if( !bReset )
			{
				bReset = true;
				ClientModule::getSingleton().sendMessage( GM_WBEND );
			}
		}


	}

	void TestModule::WhiteBoard::drawWhiteBoard()
	{
		glLineWidth( 3 );
		glColor3f( 1, 1, 1 );

		if( mvPoints.size() > 1 )
		{
			glBegin( GL_LINES );
				glVertex3f( mvPoints[0].x, mvPoints[0].y, mvPoints[0].z );
				glVertex3f( mvPoints[1].x, mvPoints[1].y, mvPoints[1].z );
				glBegin( GL_LINES );
					glVertex3f( mvPoints[mvPoints.size()-2].x, mvPoints[mvPoints.size()-2].y, mvPoints[mvPoints.size()-2].z );
					glVertex3f( mvPoints[mvPoints.size()-1].x, mvPoints[mvPoints.size()-1].y, mvPoints[mvPoints.size()-1].z );
				glEnd();
			glEnd();
		}

		if( mvPoints.size() > 4 )
		{
			for( vector<Vector3d>::iterator i = mvPoints.begin(); i != mvPoints.end()-3 && i != mvPoints.end()-2 && i != mvPoints.end()-1 && i != mvPoints.end(); i++)
			{
				Vector3d tPoints[4];
				tPoints[0] = *i;
				tPoints[1] = *(i+1);
				tPoints[2] = *(i+2);
				tPoints[3] = *(i+3);
				OpenglModule::getSingleton().drawCubicHermite( tPoints, 0, .05 );
#if 0
				glPointSize( 2 );
				glColor3f( 1, 1, 0 );
				glBegin( GL_POINTS );
					glVertex3f( tPoints[0].x, tPoints[0].y, tPoints[0].z );
					glVertex3f( tPoints[1].x, tPoints[1].y, tPoints[1].z );
					glVertex3f( tPoints[2].x, tPoints[2].y, tPoints[2].z );
					glVertex3f( tPoints[3].x, tPoints[3].y, tPoints[3].z );
				glEnd();
#endif 
			}
		}



		for( vector<vector<Vector3d>>::iterator i = wbSplines.begin(); i != wbSplines.end(); i++ )
		{
			if( i->size() > 1 )
			{
				glBegin( GL_LINES );
					glVertex3f( i->begin()->x, i->begin()->y, i->begin()->z );
					glVertex3f( (i->begin()+1)->x, (i->begin()+1)->y, (i->begin()+1)->z );
				glEnd();
				glBegin( GL_LINES );
					//glVertex3f( i->end()->x, i->end()->y, i->end()->z );
					glVertex3f( (i->end()-2)->x, (i->end()-2)->y, (i->end()-2)->z );
					glVertex3f( (i->end()-1)->x, (i->end()-1)->y, (i->end()-1)->z );
				glEnd();
			}

			if( i->size() > 4 )
				for( vector<Vector3d>::iterator j = i->begin(); j != i->end()-3; j++ )
				{
					Vector3d tPoints[4];
					tPoints[0] = *j;
					tPoints[1] = *(j+1);
					tPoints[2] = *(j+2);
					tPoints[3] = *(j+3);
					OpenglModule::getSingleton().drawCubicHermite( tPoints, 0, .05 );
				}
		}
	}

	TestModule::~TestModule()
	{
		foxLines->clear();
		if( foxLines )
			delete foxLines;
		foxLines = 0;
	}

}
